package com.study.example.gameserver.module.backpack.entity;

import com.sparrow.cache.annotation.Cache;
import com.sparrow.cache.annotation.CacheType;
import com.study.example.gameserver.module.backpack.convert.ItemsConverter;
import com.study.example.gameserver.module.entity.PlayerData;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 玩家背包数据
 */
@Cache(cacheType = CacheType.NONE)
@Entity
@Table(name = "PlayerBackPack")
public class PlayerBackPack extends PlayerData {

    @Convert(converter = ItemsConverter.class)
    @Column(name = "items", columnDefinition = "LONGTEXT")
    private ConcurrentHashMap<Long, Item> items = new ConcurrentHashMap<>();

    private transient List<Item> sortedItems;

    public void setSortedItems(List<Item> sortedItems) {
        this.sortedItems = sortedItems;
    }

    public List<Item> getSortedItems() {
        return sortedItems;
    }

    public ConcurrentHashMap<Long, Item> getItems() {
        return items;
    }

    public void addItem(Item e) {
        items.put(e.getId(), e);
    }

    public boolean removeItem(long id) {
        return items.remove(id) != null;
    }

}
